// 监听事件变化

import { useEffect } from 'react';

/**
 *  @description 统一处理表格类请求
 *  @param {target} dom windows或者dom
 *  @param {type}  事件类型
 *  @param {type}  具体的事件
 *
 **/

const useEventListener = (target, type, listener) => {
//   console.log('useEventListener', target, type, listener);
  useEffect(() => {
    const targetIsRef = target.hasOwnProperty('current');
    const currentTarget = targetIsRef ? target.current : target;

    if (currentTarget) {
      currentTarget.addEventListener(type, listener);
    }

    return () => {
      if (currentTarget) {
        currentTarget.removeEventListener(type, listener);
      }
    };
  }, []);
};

export default useEventListener;
